/**
 * @file webpack.config.js 本地调试环境配置
 * @author zhanlong
 */

'use strict';

/* eslint-disable */
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const lessToJs = require('less-vars-to-js');
const variable = lessToJs(fs.readFileSync(path.join(__dirname, 'src/assets/theme/variable.less'), 'utf8'));

const options = {
    alias: {
        'https://at.alicdn.com/t/font_zck90zmlh7hf47vi': 'https://iot-static.cdn.bcebos.com/static/font/iconfont'
    }
};


module.exports = {
    devtool: '#inline-source-map',
    cache: true,
    entry: {
        index: ['babel-polyfill', './src/entry.js']
    },

    output: {
        path: path.join(__dirname, './dist'),
        filename: '[name].bundle.js',
        publicPath: '/',
        chunkFilename: '[name]-[id].chunk.js'
    },

    plugins: [
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.DefinePlugin({
            '__DEV__': 'true',
            'process.env': {
                NODE_ENV: JSON.stringify(process.env.development || 'development')
            }
        }),
        new HtmlWebpackPlugin({
            client: 'debug',
            filename: '../dist/debug.html',
            template: 'client/views/debug.html'
        })
    ],

    resolve: {
        alias: {},
        extensions: ['.js', '.jsx', '.css', '.less', 'es6', 'es']
    },
    

    module: {
        rules: [
            {
                test: /\.jsx?$/,
                loader: 'react-hot-loader/webpack',
                exclude: /node_modules/
            },
            {
                test: /\.bundle\.jsx?$/,
                loaders: 'bundle-loader',
                include: path.resolve(__dirname, 'src/containers'),
                query: {
                    lazy: true,
                    name: '[name]'
                }
            },
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    cacheDirectory: true,
                    presets: ['es2015', 'react', 'stage-1']
                }
            },
            {
                test: path.resolve('./src/redux/reducers'),
                loader: './plugins/reducer-inject-loader',
                query: {
                    include: './src',
                    getReducerName(filePath) {
                        const reg = /^.*?containers\/(.*?)\/redux/;
                        const matches = filePath.toLowerCase().match(reg);
                        console.log(filePath.replace(/\\/g, '\/'));
                        return matches ? matches[1] : filePath.replace(/[./]/g, '_');
                    }
                }
            },
            {
                test: /\.css$/,
                exclude: /node_modules|assets/,
                loader: 'style-loader!css-loader?modules&camelCase&localIdentName=[name]__[local]-[hash:base64:5]&sourceMap=true!postcss-loader'
            },
            {
                test: /\.css$/,
                include: /assets|node_modules/,
                loader: `style-loader!css-loader!postcss-loader!./plugins/css-replace-loader?${JSON.stringify(options)}`
            },
            {
                test: /\.less/,
                loader: `style-loader!css-loader!less-loader?{"javascriptEnabled":true,sourceMap: true,"modifyVars":${JSON.stringify(variable)}}`
            },
            {
                test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
                loader: 'url-loader?limit=30000&name=[name]-[hash].[ext]'
            },
            {
                test: /\.json$/,
                loader: 'json-loader'
            }
        ]
    }
};
